/**
 * 
 */
package com.hsxc.cdz.controller.wxController.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/** 
* @author  wpfei
* @version 
* 
*/

public class HttpSLL {

	/**
	 * 
	 */
	private static HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() {
		public boolean verify(String s, SSLSession sslsession) {
			System.out.println("WARNING: Hostname is not matched for cert.");
			return true;
		}
	};

	/**
	 * Ignore Certification
	 */
	private static TrustManager ignoreCertificationTrustManger = new X509TrustManager() {

		private X509Certificate[]certificates;

	@Override public void checkClientTrusted(X509Certificate certificates[],String authType)throws CertificateException{if(this.certificates==null){this.certificates=certificates;System.out.println("init at checkClientTrusted");}}

	@Override public void checkServerTrusted(X509Certificate[]ax509certificate,String s)throws CertificateException{if(this.certificates==null){this.certificates=ax509certificate;System.out.println("init at checkServerTrusted");}

	}

	@Override public X509Certificate[] getAcceptedIssuers(){
	return null;}};

	public static String getMethod(String urlString) {

		ByteArrayOutputStream buffer = new ByteArrayOutputStream(512);
		try {
			URL url = new URL(urlString);
			/*
			 * use ignore host name verifier
			 */
			HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
			HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
			TrustManager[] tm = { ignoreCertificationTrustManger };
			SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
			sslContext.init(null, tm, new java.security.SecureRandom());

			SSLSocketFactory ssf = sslContext.getSocketFactory();
			connection.setSSLSocketFactory(ssf);

			InputStream reader = connection.getInputStream();
			byte[] bytes = new byte[512];
			
			int length = reader.read(bytes);

			do {
				buffer.write(bytes, 0, length);
				length = reader.read(bytes);
			} while (length > 0);

			System.out.println(buffer.toString());
			reader.close();

			connection.disconnect();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
		}
		String repString = new String(buffer.toByteArray());
		return repString;
	}


}
